//
//  MainViewController.swift
//  01weiboSwift
//
//  Created by 龚欣 on 15/6/24.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    func setUpUI(){
    
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKeyPath: "tabBar")
        mainTabBar.composedBtn.addTarget(self, action: "composedBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        addChildControllers()
    }
    
    func addChildControllers(){
        
        
        let path = NSBundle.mainBundle().pathForResource("VCSetting.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        do{
            let dictArray = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [[String : String]]
            for dict in dictArray{
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        }catch{
        
            print(error)
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
        
       
        
    }
    
    func addChildViewController(vcName : String , title : String, imageName : String) {
        
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        let fullName = ns + "." + vcName
        let vcCls : AnyClass = NSClassFromString(fullName)!
        let vc = vcCls.new() as! UIViewController
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_highlighted")
        
        let navi = UINavigationController(rootViewController: vc)
        addChildViewController(navi)
    }

    func composedBtnClick(){
        print("点击了中间的加号按钮")
        
        let vc = SendStatusViewController()
        let navi = UINavigationController(rootViewController: vc)
        presentViewController(navi, animated: true, completion: nil)
    }
    
    }
